package com.mybank.myaccounts.mapper;

import com.mybank.common.entity.Transaction;
import com.mybank.myaccounts.dto.TransactionDto;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

@Component
public class TransactionMapper {

    public TransactionDto toDto(Transaction transaction) {
        if (transaction == null) {
            return null;
        }

        return TransactionDto.builder()
                .transactionId(transaction.getTransactionId())
                .accountId(transaction.getAccount() != null ? transaction.getAccount().getAccountId() : null)
                .accountNumber(transaction.getAccount() != null ? transaction.getAccount().getAccountNumber() : null)
                .accountName(transaction.getAccount() != null && transaction.getAccount().getAccountType() != null ? 
                        transaction.getAccount().getAccountType().getName() : "Account")
                .transactionType(transaction.getTransactionType())
                .amount(transaction.getAmount())
                .description(transaction.getDescription())
                //.reference(transaction.getReference())
                .transactionDate(transaction.getTransactionDate())
                .transactionStatus(transaction.getTransactionStatus())
                //.category(transaction.getCategory())
                .build();
    }

    public List<TransactionDto> toDtoList(List<Transaction> transactions) {
        if (transactions == null) {
            return null;
        }

        return transactions.stream()
                .map(this::toDto)
                .collect(Collectors.toList());
    }
}